//
//  YZYNetworkManager.m
//  YZYSDKDemo
//
//  Created by 赵隆杰 on 2024/6/25.
//

#import "YZYNetworkManager.h"
#import "ZCNetworkManager.h"
#import "YZYSDKManagerHeader.h"
#import "ZCFunDataFormat.h"
#import "YZYRoleInfo.h"

@implementation YZYNetworkManager

/**
 单例模式

 @return YZYSDKManager
 */
+ (instancetype)shareManager {
    static YZYNetworkManager *shareManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[YZYNetworkManager alloc] init];
    });
    return shareManager;
}

- (instancetype)init {
    if (self = [super init]) {
    }
    return self;
}

/*
 根据token获取用户信息
 */
- (void)requestGetUserByTokenSuccess:(NMSuccessBlock)success failure:(NMFailureBlock)failure {
    NSDictionary *params = @{};
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_getUserByToken) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        
        NSString *code = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        NSDictionary *resultDic = [NSDictionary dictionaryWithDictionary:surperdic[@"data"]];
        if ([code isEqualToString:@"200"]) {
            success(resultDic);
        }
        
    } failure:^(NSString *error) {
        
    }];
}

/*
 一键登录
 */
- (void)requestQuickAuthLoginWithToken:(NSString *)token Success:(NMSuccessBlock)success failure:(NMFailureBlock)failure {
    NSString *package = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSDictionary *params = @{
        @"token": token,
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"packageName": package,
        @"channelNo": [ZCFunDataFormat GetChanneNo]
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_loginDirectly) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        
        NSString *code = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        NSDictionary *resultDic = [NSDictionary dictionaryWithDictionary:surperdic[@"data"]];
        if ([code isEqualToString:@"200"]) {
            success(resultDic);
        }
        
    } failure:^(NSString *error) {
        
    }];
}

/*
 创建订单
 */
- (void)requestPlatformOrderInfoCreateOrderWithPayMoney:(NSString *)payMoney gameOrderSn:(NSString *)gameOrderSn remark:(NSString *)remark sonUserId:(NSString *)sonUserId userId:(NSString *)userId roleInfo:(YZYRoleInfo *)roleInfo completeBlock:(void(^)(id data))complete {
    NSDictionary *params = @{
        @"amountReduce": @0,
        @"amountShould": payMoney,
        @"cpOrderNo": gameOrderSn,
        @"remarks": remark,
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"roleId": roleInfo.super_role_id,//这里传平台角色id
        @"roleName": roleInfo.role_name,
        @"serviceId": roleInfo.service_id,
        @"serviceName": roleInfo.service_name,
        @"sonUserId": sonUserId,
        @"userId": userId,
        @"osType": @"2"
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_createOrder) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        
        NSString *code = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        NSDictionary *resultDic = [NSDictionary dictionaryWithDictionary:surperdic[@"data"]];
        if ([code isEqualToString:@"200"]) {
            complete(resultDic);
        }
        
    } failure:^(NSString *error) {
        
    }];
}


/*
 支付ID映射
 */
- (void)requestBillingPointMappingWithGoodID:(NSString *)goodID Success:(NMSuccessBlock)success failure:(NMFailureBlock)failure {
    NSString *package = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSDictionary *params = @{
        @"goodNo": goodID,
        @"gameId": [ZCFunDataFormat GetPlatformGameID]
    };
    [ZCNetworkManager getWithURL:YZY_AppendUrl(YZY_pay_billingPointMapping) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        
        NSString *code = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        NSDictionary *resultDic = [NSDictionary dictionaryWithDictionary:surperdic[@"data"]];
        if ([code isEqualToString:@"200"]) {
            success(resultDic);
        }
        
    } failure:^(NSString *error) {
        
    }];
}

/*
 支付验证接口
 */
- (void)requestIosPayVerifyWithGoodID:(NSString *)goodID chooseEnv:(NSNumber *)chooseEnv orderNo:(NSString *)orderNo receipt:(NSString *)receipt completeBlock:(void(^)(id data))complete {
    NSDictionary *params = @{
        @"productId": goodID,//商品id
        @"chooseEnv": chooseEnv,//是否是真实环境 1真实环境 0沙箱环境
        @"orderNo": orderNo,//订单号
        @"receipt": receipt//苹果传递给客户端支付成功的值
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_pay_verify) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        
        NSString *code = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        NSDictionary *resultDic = [NSDictionary dictionaryWithDictionary:surperdic[@"data"]];
        if ([code isEqualToString:@"200"]) {
            complete(resultDic);
        }
        
    } failure:^(NSString *error) {
        
    }];
}

- (void)requestWechatGetInfoSuccess:(NMSuccessBlock)success failure:(NMFailureBlock)failure {
    
    [ZCNetworkManager getWithURL:QPJH_AppendUrl_Api_v1(YZY_wechat_api_getInfo) Params:nil success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        NSString *surpercode = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
        if ([surpercode isEqualToString:@"200"]) {
            NSDictionary *result = (NSDictionary *)surperdic[@"data"];
            self.wx_appid = result[@"appid"];
            self.wx_path = result[@"path"];
            success(result);
        } else {
            failure(@"");
        }
    } failure:^(NSString *error) {
        failure(@"");
    }];
}

@end
